package com.net.oil.message;

import android.graphics.Color;
import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.message.adapter.MessageRecyclerViewAdapter;
import com.net.oil.message.bean.MessageBean;
import com.net.oil.message.contract.MessageContract;
import com.net.oil.message.presenter.MessagePresenter;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MessageActivity extends BaseActivity<MessagePresenter> implements MessageContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.message_recycler_view)
    SwipeMenuRecyclerView messageRecyclerView;


    private MessageRecyclerViewAdapter adapter;
    private List<MessageBean.ListBean> messageList;
    private int deletePosition = -1;

    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        presenter = new MessagePresenter(this);
        initView();
        initData();
    }

    private void initData() {
        presenter.getMessageList(page, rows);
    }

    private void initView() {
        toolbarTitle.setText(R.string.message_inform);
        adapter = new MessageRecyclerViewAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        messageRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        messageRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);

        messageRecyclerView.setLayoutManager(layoutManager);
        messageRecyclerView.setAdapter(adapter);

        messageRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (!messageRecyclerView.canScrollVertically(1) && haveMore && page > 1) {
                    messageRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            adapter.changeMoreStatus(Constant.LOADING_MORE);
                        }
                    });

                    presenter.getMessageList(page, rows);
                }
            }
        });
    }

    @OnClick(R.id.toolbar_go_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取消息列表成功
     *
     * @param messageBean 消息数据
     */
    @Override
    public void getMessageListSuccess(MessageBean messageBean) {
        if (messageBean != null) {
            this.messageList = messageBean.getList();
            if (messageBean.getList() != null && messageBean.getList().size() > 0) {
                if (page == 1) {
                    adapter.updateData(messageBean.getList());
                } else if (page > 1) {
                    adapter.loadMore(messageBean.getList());
                }
                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            } else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }
        }
    }

    /**
     * 获取消息列表失败
     */
    @Override
    public void getMessageListFailed() {
    }

    /**
     * 删除消息成功
     */
    @Override
    public void deleteMessageSuccess() {
        adapter.deleteItem(deletePosition);
    }

    /**
     * 删除消息失败
     */
    @Override
    public void deleteMessageFailed() {

    }

    /**
     * 菜单创建器，在Item要创建菜单的时候调用。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.dimen_70dp);

            // 1. MATCH_PARENT 自适应高度，保持和Item一样高;
            // 2. 指定具体的高，比如80;
            // 3. WRAP_CONTENT，自身高度，不推荐;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
//            int height = getResources().getDimensionPixelSize(R.dimen.dimen_80dp);

            // 添加右侧的，如果不添加，则右侧不会出现菜单。
            SwipeMenuItem deleteItem = new SwipeMenuItem(MessageActivity.this)
                    .setBackground(R.drawable.selector_red)
                    .setImage(R.drawable.ic_action_delete)
                    .setText("删除")
                    .setTextColor(Color.WHITE)
                    .setWidth(width)
                    .setHeight(height);
            swipeRightMenu.addMenuItem(deleteItem);// 添加菜单到右侧。


        }
    };

    /**
     * RecyclerView的Item的Menu点击监听。
     */
    private SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
        @Override
        public void onItemClick(SwipeMenuBridge menuBridge) {
            menuBridge.closeMenu();
            deletePosition = menuBridge.getAdapterPosition();
            int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
            if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                if (adapter.getItemCount() > 0){
                    presenter.deleteMessage(adapter.getItemMessageId(deletePosition));      //删除消息
                }
            }
        }
    };
}
